package com.yanggeng.demo.beginnerguidedemo.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;

import com.yanggeng.demo.beginnerguidedemo.R;
import com.yanggeng.demo.beginnerguidedemo.fragment.ActivityFragment;
import com.yanggeng.demo.beginnerguidedemo.fragment.HomeFragment;
import com.yanggeng.demo.beginnerguidedemo.fragment.MineFragment;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by yanggeng on 2017/7/6.
 */

public class DrawerLayoutActivity extends AppCompatActivity {


    NavigationView nav;
    DrawerLayout drawerLayout;

    private Fragment homeFragment, mineFragment, lastShowFragment, activityFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drawerlayout);

        nav = (NavigationView) findViewById(R.id.nav);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        findViewById(R.id.ll_end).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
        initNavigationView();
        changeHome();
    }

    private void initNavigationView() {

//        nav.getHeaderView(0).findViewById(R.id.****); //获取侧边栏头部控件

        nav.setItemIconTintList(null);

        //侧边栏菜单点击事件
        nav.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.nav_home:
                        changeHome();
                        break;
                    case R.id.nav_activity:
                        changeActivity();
                        break;
                    case R.id.nav_message:

                        break;
                    case R.id.nav_mine:
                        changeMine();
                        break;
                }
                drawerLayout.closeDrawer(GravityCompat.START);
                return true;
            }
        });
    }

    private void changeMine() {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction ft = manager.beginTransaction();
        if(mineFragment == null) {
            mineFragment = new MineFragment();
            ft.add(R.id.drawer_content, mineFragment);
        } else {
            if (lastShowFragment == mineFragment) {
                drawerLayout.closeDrawer(GravityCompat.START);
                return;
            }
            ft.show(mineFragment);
        }
        if(lastShowFragment != null) {
            ft.hide(lastShowFragment);
        }
        ft.commit();
        lastShowFragment = mineFragment;
        drawerLayout.closeDrawer(GravityCompat.START);
    }

    private void changeHome() {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction ft = manager.beginTransaction();
        if(homeFragment == null) {
            homeFragment = new HomeFragment();
            ft.add(R.id.drawer_content, homeFragment);
        } else {
            if (lastShowFragment == homeFragment) {
                drawerLayout.closeDrawer(GravityCompat.START);
                return;
            }
            ft.show(homeFragment);
        }
        if(lastShowFragment != null) {
            ft.hide(lastShowFragment);
        }
        ft.commit();
        lastShowFragment = homeFragment;
        drawerLayout.closeDrawer(GravityCompat.START);
    }

    private void changeActivity() {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction ft = manager.beginTransaction();
        if(activityFragment == null) {
            activityFragment = new ActivityFragment();
            ft.add(R.id.drawer_content, activityFragment);
        } else {
            if (lastShowFragment == activityFragment) {
                drawerLayout.closeDrawer(GravityCompat.START);
                return;
            }
            ft.show(activityFragment);
        }
        if(lastShowFragment != null) {
            ft.hide(lastShowFragment);
        }
        ft.commit();
        lastShowFragment = activityFragment;
        drawerLayout.closeDrawer(GravityCompat.START);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public DrawerLayout getDrawerLayout() {
        return drawerLayout;
    }
}
